<!-- 准新生登记 -->
<template>
  <zh-app style="font-family: '阿里巴巴普惠体'">
    <!-- 导航栏 -->
    <zh-navbar title="准新生登记" color="#85a5ff" linear="right" />
    <zh-spin :load="data.loading" tip="信息加载中，请耐心等候">
      <view v-if="!data.regFlag && !data.loading">
        <zh-result
          status="lock"
          color="amber"
          :showBtn="false"
          :title="data.operation.title"
          :subTitle="data.operation.subTitle"
        ></zh-result>
        <view class="flex-row-center-center">
          <zh-button
            label="返回"
            @click="goBack"
            linear="right"
          ></zh-button>
        </view>
      </view>
      <!-- 表单 -->
      <zh-sheet
        :round="3"
        v-if="data.regFlag"
        :margin="[30, 20]"
        :shadow="3"
        :padding="[20, 20]"
        text
        color="orange"
      >
        <zh-text class="text-weight-b" label="温馨提示:"></zh-text>
        <zh-text
          v-if="!data.childName"
          style="text-indent: 2em; padding-top: 10rpx"
          label="您暂未登记适龄子女信息,请登记（本入口为义务段新生登记入口，幼儿园入学无需登记）"
        ></zh-text>
        <zh-text
          v-if="data.childName"
          style="text-indent: 2em; padding-top: 10rpx"
          >您已登记<span style="color: #3b5cf0; font-weight: 600">{{
            data.childName
          }}</span
          >信息,</zh-text
        >
        <view
          v-if="data.childName"
          class="flex-row-center-start"
          style="padding-top: 10rpx"
        >
          <zh-text style="text-indent: 2em" label="前往"></zh-text>
          <zh-sheet
            :width="elderFlag ? 300 : 175"
            :margin="[0, 0]"
            :padding="[10, 10]"
            color="blue"
            :shadow="4"
            :round="2"
            ><zh-text label="当年适龄子女" @click="gotoMy"></zh-text
          ></zh-sheet>
          <zh-text label="查看"></zh-text>
        </view>
      </zh-sheet>
      <view class="container shadow" v-if="data.regFlag">
        <h3>基本信息</h3>
        <zh-form
          ref="formRef"
          @submit="submit"
          v-model="data.form"
          inputAlign="right"
          :label-width="200"
          :margin="[0, 24]"
          :padding="[10, 0]"
        >
          <zh-form-select
            field="areaCode"
            @confirm="onConfirm"
            v-if="data.areaCodeDict.length > 1"
            :rules="[{ required: true, message: '请选择想要就读区县' }]"
            placeholder="请选择想要就读区县"
            v-model="data.form.areaCode"
            :options="data.areaCodeDict"
            label="就读区县"
            required
            :transprent="true"
          ></zh-form-select>
          <zh-form-input
            field="fullName"
            :rules="[{ required: true, message: '请输入学生姓名' }]"
            placeholder="请输入学生姓名"
            v-model="data.form.fullName"
            label="学生姓名"
            required
            showClear
            :transprent="true"
          ></zh-form-input>
          <zh-form-select
            field="cardType"
            :showClear="false"
            :rules="[{ required: true, message: '请选择学生证件类型' }]"
            placeholder="请选择学生证件类型"
            v-model="data.form.cardType"
            :options="data.cardTypeDict"
            label="学生证件类型"
            required
            :transprent="true"
          ></zh-form-select>
          <zh-form-input
            field="idcard"
            :type="data.form.cardType === '01' ? 'idcard' : 'text'"
            :rules="[{ required: true, message: '请输入学生证件号码' }]"
            placeholder="请输入学生证件号码"
            v-model="data.form.idcard"
            label="学生证件号码"
            @input="checkAge"
            required
            showClear
            :transprent="true"
          ></zh-form-input>
          <zh-form-datetime
            v-if="data.form.cardType !== '01'"
            field="birthYmd"
            :rules="[{ required: true, message: '请输入学生出生日期' }]"
            placeholder="请输入学生出生日期"
            v-model="data.form.birthYmd"
            label="出生日期"
            required
            showClear
            model="day"
            @confirm="checkAge"
            :transprent="true"
          ></zh-form-datetime>
          <zh-form-select
            v-if="data.form.cardType !== '01'"
            field="sex"
            :rules="[{ required: true, message: '请选择学生性别' }]"
            placeholder="请选择学生性别"
            v-model="data.form.sex"
            :options="data.sexDict"
            label="性别"
            required
            showClear
            :transprent="true"
          ></zh-form-select>
          <zh-form-input
            v-if="data.myCheckInfo.showOtherInfo"
            field="address"
            :rules="[{ required: true, message: '请输入学生现居住地' }]"
            placeholder="请输入学生现居住地"
            v-model="data.form.address"
            label="现居住地"
            required
            showClear
            :transprent="true"
          ></zh-form-input>
          <zh-form-input
            v-if="data.myCheckInfo.showOtherInfo"
            field="householdPlace"
            :rules="[{ required: true, message: '请输入学生户籍地' }]"
            placeholder="请输入学生户籍地"
            v-model="data.form.householdPlace"
            label="户籍地"
            required
            showClear
            :transprent="true"
          ></zh-form-input>
          <zh-form-input
            field="fatherName"
            :rules="[{ required: false, message: '请输入父亲(监护人)姓名' }]"
            placeholder="请输入父亲(监护人)姓名"
            v-model="data.form.fatherName"
            label="父亲(监护人)姓名"
            showClear
            :transprent="true"
          ></zh-form-input>
          <zh-form-select
            field="fatherCardType"
            :showClear="false"
            :rules="[
              { required: false, message: '请输入父亲(监护人)证件类型' },
            ]"
            placeholder="请输入父亲(监护人)证件类型"
            v-model="data.form.fatherCardType"
            :options="data.cardTypeDict"
            label="父亲(监护人)证件类型"
            :transprent="true"
          ></zh-form-select>
          <zh-form-input
            field="fatherIdcard"
            :type="data.form.fatherCardType === '01' ? 'idcard' : 'text'"
            :rules="[
              { required: false, message: '请输入父亲(监护人)证件号码' },
            ]"
            placeholder="请输入父亲(监护人)证件号码"
            v-model="data.form.fatherIdcard"
            label="父亲(监护人)身份证"
            showClear
            :transprent="true"
          ></zh-form-input>
          <zh-form-input
            v-if="data.myCheckInfo.showOtherInfo"
            field="fatherCellphone"
            :rules="[{ required: false, message: '请输入父亲(监护人)手机' }]"
            number
            placeholder="请输入父亲(监护人)手机"
            v-model="data.form.fatherCellphone"
            label="父亲(监护人)手机"
            showClear
            :transprent="true"
          ></zh-form-input>
          <zh-form-input
            field="motherName"
            :rules="[{ required: false, message: '请输入母亲姓名' }]"
            placeholder="请输入母亲姓名"
            v-model="data.form.motherName"
            label="母亲姓名"
            showClear
            :transprent="true"
          ></zh-form-input>
          <zh-form-select
            field="motherCardType"
            :showClear="false"
            :rules="[{ required: false, message: '请输入母亲证件类型' }]"
            placeholder="请输入母亲证件类型"
            v-model="data.form.motherCardType"
            :options="data.cardTypeDict"
            label="母亲证件类型"
            :transprent="true"
          ></zh-form-select>
          <zh-form-input
            field="motherIdcard"
            :type="data.form.motherCardType === '01' ? 'idcard' : 'text'"
            :rules="[{ required: false, message: '请输入母亲证件号码' }]"
            placeholder="请输入母亲证件号码"
            v-model="data.form.motherIdcard"
            label="母亲证件号码"
            showClear
            :transprent="true"
          ></zh-form-input>
          <zh-form-input
            v-if="data.myCheckInfo.showOtherInfo"
            field="motherCellphone"
            :rules="[{ required: false, message: '请输入母亲手机' }]"
            type="number"
            placeholder="请输入母亲手机"
            v-model="data.form.motherCellphone"
            label="母亲手机"
            showClear
            :transprent="true"
          ></zh-form-input>
          <zh-form-input
            field="remarks"
            layout="vertical"
            inputAlign="left"
            v-model="data.form.remarks"
            :height="160"
            label="备注"
            type="textarea"
            placeholder="请输入备注信息"
            showClear
            :transprent="true"
          ></zh-form-input>
          <zh-form-upload
            field="fileList"
            v-model:fileList="data.form.fileList"
            v-model="data.form.fileInfo"
            label="材料上传"
            required
            :defaultValue="[
              { remarks: '户口本第一页' },
              { remarks: '户口本户主页' },
              { remarks: '户口本孩子页' },
            ]"
            :fileType="['image']"
            :rows="elderFlag?1:3"
            :width="620"
            :rules="[
              { required: true, message: '请上传户口本等照片，不少于3张' },
            ]"
          >
            <template v-slot:desc>
              <zh-text
                color="grey-darken-2"
                :font-size="22"
                label="1.新生和父母在同一户口本，需上传①户主页，②父亲页， ③母亲页，④新生页；"
              ></zh-text>
              <zh-text
                color="grey-darken-2"
                :font-size="22"
                label="2.新生和父母未在同一户口本：需上传①新生所在户主页，②父亲页，③母亲页，④新生页，⑤父母结婚证或孩子出生证；"
              ></zh-text>
              <zh-text
                color="grey-darken-2"
                :font-size="22"
                label="3.上传照片的信息需要完整、清晰，不可缺角。"
              ></zh-text>
            </template>
          </zh-form-upload>

          <zh-form-item :border="false">
            <view class="flex flex-row">
              <view class="flex-1 mr-32" style="margin: 0 auto">
                <zh-button
                  form-type="submit"
                  :disabled="data.btnDisabled"
                  :loading="data.btnDisabled"
                  label="提交信息"
                  block
                  color="#85a5ff"
                  linear="right"
                ></zh-button>
              </view>
            </view>
          </zh-form-item>
        </zh-form>
      </view>
    </zh-spin>
    <zh-message ref="messageRef" :duration="2000"></zh-message>
    <zh-modal
      title="温馨提醒"
      content="登记成功，请等待工作人员审核,审核结果可在当年适龄子女查看"
      cancelText="关闭页面"
      okText="再登记一个"
      :overlayClick="false"
      v-model:show="data.showSuccess"
      @ok="onOk"
      @cancel="onCancel"
    ></zh-modal>
  </zh-app>
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted } from "vue";
import * as util from "@/tool/function/util";
import { onLoad } from "@dcloudio/uni-app";
import { system } from "@/store";
// ------------------接收传入参数(组件)-------------------

// ------------------钩子函数----------------------------
// 初始化
onLoad((options: any) => {
  if (options.areaCode) {
    data.form.areaCode = options.areaCode;
  }
  getAreaCode();
});

// ------------------变量区------------------------------
const messageRef = ref();
const elderFlag = computed(() => {
  return system.uiStyle === "elder";
});
const formRef = ref();
// 主数据
const data = reactive({
  myCheckInfo: {} as any,
  loading: false,
  //是否可以登记
  regFlag: true,
  operation: { title: "系统异常", subTitle: "请退出重新打开页面" },
  // 表单
  form: {
    fullName: "",
    cardType: "01",
    idcard: "",
    sex: "",
    birthYmd: "",
    motherName: "",
    motherCardType: "01",
    motherIdcard: "",
    motherCellphone: "",
    fatherName: "",
    fatherCardType: "01",
    fatherIdcard: "",
    fatherCellphone: "",
    address: "",
    householdPlace: "",
    areaCode: system.areaCode,
    remarks: "",
    fileList: [],
  } as any,
  sexDict: [
    {
      dictKey: "1",
      dictValue: "男",
    },
    {
      dictKey: "2",
      dictValue: "女",
    },
  ],
  cardTypeDict: [
    {
      dictKey: "01",
      dictValue: "身份证",
    },
    {
      dictKey: "99",
      dictValue: "其他证件",
    },
  ],
  areaCodeDict: [] as any,
  btnDisabled: false,
  showSuccess: false,
  childName: "", //已登记学生信息
});

// ------------------方法区------------------------------
const onConfirm = () => {
  //行政区划变动选择
  getOp();
};
const getOpCheck = () => {
  let params = { areaCode: data.form.areaCode };
  $api.common.send.post("edustudentenrollpre0016", params).then((res: any) => {
    data.myCheckInfo = res.result;
    getChild();
  });
};
const getChild = () => {
  //查询当年适龄子女信息
  let params = {};
  $api.common.send
    .post("edustudentenrollpre0015", params)
    .then((res: any) => {
      let childList = res.result || [];
      let name = "";
      childList.forEach((item :any) => {
        if (name) {
          name = name + "，" + item.fullName;
        } else {
          name = item.fullName;
        }
      });
      data.childName = name;
    })
    .catch(() => {
      data.childName = "";
    });
};
//获取业务期
const getOp = () => {
  let params = {
    operating: "YW018",
    classify: "2",
    areaCode: data.form.areaCode,
  };
  data.loading = true;
  $api.common.send
    .post("OperationCache0003", params)
    .then((res: any) => {
      let result = res.result;
      data.operation.title = "不能办理业务";
      if (result && result.flag) {
        data.regFlag = true;
        getOpCheck();
      } else {
        if (result) {
          if (result.type === "1" && result.messagePrompts) {
            data.operation.subTitle = result.messagePrompts;
          } else if (result.type === "2" && result.messageTip) {
            data.operation.subTitle = result.messageTip;
          } else {
            data.operation.subTitle =
              result.year + "年度义务教育阶段招生工作尚未开始";
          }
        }
        data.regFlag = false;
      }
      data.loading = false;
    })
    .catch(() => {
      data.loading = false;
    });
};
//校验年龄
const checkAge = () => {
  if (!data.form.cardType) {
    return;
  }
  if (data.form.cardType === "01") {
    if (!data.form.idcard) {
      return;
    } else {
      if (data.form.idcard.length !== 18) {
        return;
      }
    }
  } else {
    if (!data.form.birthYmd) {
      return;
    }
  }
  let params = {
    idcard: data.form.idcard,
    cardType: data.form.cardType,
    birthYmd: data.form.birthYmd,
  };
  $api.common.send.post("edustudentenrollpre0022", params).then((res: any) => {
    if (!res.result) {
      let messageData: any = {};
      messageData.model = "warn";
      messageData.text = "学生不在适龄年龄段！";
      messageRef.value.show(messageData);
    }
  });
};
//获取可选择的行政区划
const getAreaCode = () => {
  data.loading = true;
  $api.common.send
    .post("dict0028", {})
    .then((res: any) => {
      data.areaCodeDict = res.result || [];
      if (data.areaCodeDict.length === 1) {
        data.form.areaCode = data.areaCodeDict[0].dictKey;
        getOp();
      } else if (data.areaCodeDict.length > 1) {
        let flag = false;
        if(data.form.areaCode){
          data.areaCodeDict.forEach((item :any) => {
            if (item.dictKey == data.form.areaCode) {
              flag = true;
              getOp();
            }
          });
        }
       
        if (!flag) {
          data.form.areaCode = "";
          data.loading = false;
        }
      }
    })
    .catch(() => {
      data.loading = false;
    });
};
// 提交
const submit = (e: any) => {
  if (e.validate) {
    if (data.form.fileList.length < 3) {
      let messageData: any = {};
      messageData.model = "warn";
      messageData.text = "请上传至少三张相关照片材料！";
      messageRef.value.show(messageData);
      return;
    } else {
      let i = 0;
      data.form.fileList.forEach((item :any) => {
        if (item.showId) {
          i = i + 1;
        }
      });
      if (i < 3) {
        let messageData: any = {};
        messageData.model = "warn";
        messageData.text = "请上传至少三张相关照片材料！";
        messageRef.value.show(messageData);
        return;
      }
    }
    if (data.form.cardType === "01" && !util.isIdCard(data.form.idcard)) {
      let messageData: any = {};
      messageData.model = "warn";
      messageData.text = "请输入正确的学生身份证！";
      messageRef.value.show(messageData);
      return;
    }
    let fimalyFlag = false;
    if (data.form.fatherIdcard) {
      if (
        data.form.fatherCardType === "01" &&
        !util.isIdCard(data.form.fatherIdcard)
      ) {
        let messageData: any = {};
        messageData.model = "warn";
        messageData.text = "请输入正确的父亲（监护人）身份证！";
        messageRef.value.show(messageData);
        return;
      }
      fimalyFlag = true;
    }
    if (data.form.fatherCellphone) {
      if (!util.isPhone(data.form.fatherCellphone)) {
        let messageData: any = {};
        messageData.model = "warn";
        messageData.text = "请输入正确的父亲（监护人）手机号码！";
        messageRef.value.show(messageData);
        return;
      }
    }
    if (data.form.motherCellphone) {
      if (!util.isPhone(data.form.motherCellphone)) {
        let messageData: any = {};
        messageData.model = "warn";
        messageData.text = "请输入正确的母亲手机号码！";
        messageRef.value.show(messageData);
        return;
      }
    }
    if (data.form.motherIdcard) {
      if (
        data.form.motherCardType === "01" &&
        !util.isIdCard(data.form.motherIdcard)
      ) {
        let messageData: any = {};
        messageData.model = "warn";
        messageData.text = "请输入正确的母亲身份证！";
        messageRef.value.show(messageData);
        return;
      }
      fimalyFlag = true;
    }
    if (!fimalyFlag) {
      let messageData: any = {};
      messageData.model = "warn";
      messageData.text = "父亲（监护人）或母亲信息至少输入一方！";
      messageRef.value.show(messageData);
      return;
    }
    //校验通过
    data.btnDisabled = true;
    $api.common.send
      .post("edustudentenrollpre0010", data.form)
      .then((res: any) => {
        //保存成功后提示
        data.showSuccess = true;
        formRef.value.reset();
        data.btnDisabled = false;
      })
      .catch(() => {
        data.btnDisabled = false;
      });
  }
};
const onCancel = () => {
  $router.back(1);
};
const goBack=()=>{
  $router.back(1)
}
const onOk = () => {
  data.showSuccess = false;
};
const gotoMy = () => {
  $router.replace("/pages/edu/theRightChild/theRightChild", {});
};
</script>
<style lang="scss" scoped>
.container {
  width: 630rpx;
  padding: 30rpx;
  margin: 20rpx 30rpx 20rpx 30rpx;
  border-radius: 0.5rem;
  background-color: #ffffff;
}
</style>
